package com.lsp.base.view.titlebar;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.lsp.base.R;
import com.lsp.base.R2;

/**
 * @author com.lsp.ibxs
 * @created 16/11/17 17:42
 */
public class TitleBar extends RelativeLayout implements TitleBarInterface{
  @BindView(R2.id.imv_backfinish) ImageView imvBackfinish;
  @BindView(R2.id.ll_backfinish) RelativeLayout llBackfinish;
  @BindView(R2.id.tv_center) TextView tvCenter;
  @BindView(R2.id.tv_right) TextView tvRight;
  @BindView(R2.id.imv_right) ImageView imvRight;
  @BindView(R2.id.rl_right) RelativeLayout rlRight;
  private Context mContext;

  public TitleBar(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.mContext = context;
    LayoutInflater.from(mContext).inflate(R.layout.layout_title_bar, this);
  }

  @Override protected void onFinishInflate() {
    super.onFinishInflate();
    ButterKnife.bind(this);
    initView();
  }

  private void initView() {

  }

  /**
   * 设置标题左图标
   */
  @Override
  public void setTitleLeft(int imgRes, OnClickListener mOnClickListener) {
    if (imgRes > 0) {
      imvBackfinish.setImageResource(imgRes);
      imvBackfinish.setVisibility(VISIBLE);
    } else {
      imvBackfinish.setVisibility(GONE);
    }
    if (null != mOnClickListener) {
      llBackfinish.setOnClickListener(mOnClickListener);
    }
  }

  /**
   * 设置标题中间文本
   */
  @Override
  public void setTitleCenter(String text, OnClickListener mOnClickListener) {
    if (!TextUtils.isEmpty(text)) {
      tvCenter.setText(text);
      tvCenter.setVisibility(VISIBLE);
    } else {
      tvCenter.setVisibility(GONE);
    }
    if (null != mOnClickListener) {
      tvCenter.setOnClickListener(mOnClickListener);
    }
  }

  /**
   * 设置标题右边文本 & 图片
   */
  @Override
  public void setTitleRight(String text, int imgRes, OnClickListener mOnClickListener) {
    if (!TextUtils.isEmpty(text)) {
      tvRight.setVisibility(VISIBLE);
      tvRight.setText(text);
    } else {
      tvRight.setVisibility(GONE);
    }
    if (imgRes > 0) {
      imvRight.setVisibility(VISIBLE);
      imvRight.setImageResource(imgRes);
    } else {
      imvRight.setVisibility(GONE);
    }

    if (mOnClickListener != null) {
      rlRight.setOnClickListener(mOnClickListener);
    }
  }

  /**
   * 隐藏titlebar
   */
  public void hiddenTitleBar() {
    setVisibility(GONE);
  }
}
